<template>
  <el-card class="main-card">
    <!-- 标题 -->
    <div class="title">{{ this.$route.name }}</div>
    <div class="operation-container">
      <el-button
        type="primary"
        size="small"
        icon="el-icon-plus"
        @click="openModel(null)"
      >
        新增菜单
      </el-button>
      <!-- 数据筛选 -->
      <div style="margin-left:auto">
        <el-input
          v-model="keywords"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="请输入菜单名"
          style="width:200px"
          @keyup.enter.native="listMenus"
        />
        <el-button
          type="primary"
          size="small"
          icon="el-icon-search"
          style="margin-left:1rem"
          @click="listMenus"
        >
          搜索
        </el-button>
      </div>
    </div>
    <!-- 权限列表 -->
    <el-table
      v-loading="loading"
      :data="menuList"
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <!-- 菜单名称 -->
      <el-table-column prop="name" label="菜单名称" width="140" />
      <!-- 菜单icon -->
      <el-table-column prop="icon" align="center" label="图标" width="100">
        <template slot-scope="scope">
          <i :class="'iconfont ' + scope.row.icon" />
        </template>
      </el-table-column>
      <!-- 菜单排序 -->
      <el-table-column
        prop="orderNum"
        align="center"
        label="排序"
        width="100"
      />
      <!-- 访问路径 -->
      <el-table-column prop="path" label="访问路径" />
      <!-- 组件路径 -->
      <el-table-column prop="component" label="组件路径" />
      <!-- 是否隐藏 -->
      <el-table-column prop="isHidden" label="隐藏" align="center" width="80">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isHidden"
            active-color="#13ce66"
            inactive-color="#F4F4F5"
            :active-value="1"
            :inactive-value="0"
            @change="changeHidden(scope.row)"
          />
        </template>
      </el-table-column>
      <!-- 创建时间 -->
      <el-table-column
        prop="createTime"
        label="创建时间"
        align="center"
        width="150"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" style="margin-right:5px" />
          {{ scope.row.createTime | date }}
        </template>
      </el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="openModel(scope.row.id)"
            v-if="
              scope.row.parentId == -1 &&
              scope.row.name.indexOf('首页') == -1 &&
              scope.row.name.indexOf('中心') == -1
            "
          >
            <i class="el-icon-plus" /> 新增
          </el-button>
          <el-button type="text" size="mini" @click="openModel(scope.row)">
            <i class="el-icon-edit" /> 修改
          </el-button>
          <el-popconfirm
            title="确定删除吗？"
            style="margin-left:10px"
            @confirm="deleteMenu(scope.row.id)"
          >
            <el-button size="mini" type="text" slot="reference">
              <i class="el-icon-delete" /> 删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 新增模态框 -->
    <el-dialog :visible.sync="addMenu" width="30%" top="12vh">
      <div class="dialog-title-container" slot="title" ref="menuTitle" />
      <el-form label-width="80px" size="medium" :model="menuForm">
        <!-- 菜单类型 -->
        <el-form-item label="菜单类型" v-show="updateMenu">
          <el-radio-group v-model="isCatalog" v-show="!type">
            <el-radio :label="false">子菜单</el-radio>
          </el-radio-group>
          <el-radio-group v-model="isCatalog" v-show="type">
            <el-radio :label="true">主菜单</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 菜单名称 -->
        <el-form-item label="菜单名称">
          <el-input v-model="menuForm.name" style="width:220px" />
        </el-form-item>
        <!-- 菜单图标 -->
        <el-form-item label="菜单图标">
          <div @click="showIcon = true">
            <el-input
              :prefix-icon="'iconfont ' + menuForm.icon"
              v-model="menuForm.icon"
              style="width: 220px"
            />
          </div>
          <div class="menu-container" v-show="showIcon">
            <div
              v-for="(item, index) of iconList"
              :key="index"
              @click="checkIcon(item)"
            >
              <i :class="'iconfont ' + item" /> {{ item }}
            </div>
          </div>
        </el-form-item>
        <!-- 组件路径 -->
        <el-form-item label="组件路径" v-show="!isCatalog || !updateMenu">
          <el-input v-model="menuForm.component" style="width:220px" />
        </el-form-item>
        <!-- 路由地址 -->
        <el-form-item label="访问路径">
          <el-input v-model="menuForm.path" style="width:220px" />
        </el-form-item>
        <!-- 显示排序 -->
        <el-form-item label="显示排序">
          <el-input-number
            v-model="menuForm.orderNum"
            controls-position="right"
            @change="handleChange(menuForm.orderNum)"
            :min="1"
            :max="10"
          />
        </el-form-item>
        <!-- 显示状态 -->
        <el-form-item label="显示状态">
          <el-radio-group v-model="menuForm.isHidden">
            <el-radio :label="0">显示</el-radio>
            <el-radio :label="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="addMenu = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdateMenu">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
export default {
  created() {
    this.listMenus();
  },
  data() {
    return {
      keywords: "",
      loading: true,
      type: true,
      addMenu: false,
      updateMenu: false,
      showIcon: false,
      isCatalog: true,
      menuList: [],
      menuForm: {
        id: null,
        isHidden: null,
        name: "",
        icon: "",
        component: "",
        path: "",
        isCatalog: null,
        orderNum: null,
        isDisable: null,
        parentId: null,
        createTime: null,
        updateTime: null,
      },
      icon: "",
      iconList: [
                "el-icon-myshouye",
                "el-icon-myfabiaowenzhang",
                "el-icon-myyonghuliebiao",
                "el-icon-myxiaoxi",
                "el-icon-myliuyan",
                "el-icon-mywenzhang-copy",
                "el-icon-myshezhi",
                "el-icon-myuser",
                "el-icon-myfenlei",
                "el-icon-myicontag",
                "el-icon-mypinglunzu",
                "el-icon-myjiaoseliebiao",
                "el-icon-myxitong",
                "el-icon-mycaidan",
                "el-icon-mydashujukeshihuaico-",
                "el-icon-myguanyuwo",
                "el-icon-mydaohanglantubiao_quanxianguanli",
                "el-icon-mypinglun",
                "el-icon-myfabusekuai",
                "el-icon-myiconfontdongtaidianji",
                "el-icon-myimage-fill",
                "el-icon-myzhaopian",
                "el-icon-myhuishouzhan",
                "el-icon-myyemianpeizhi"
      ]
    };
  },
  methods: {
    handleChange(orderNum) {
      this.menuForm.orderNum = orderNum;
    },
    changeHidden(menu) {
      let param = new URLSearchParams();
      param.append("isHidden", menu.isHidden);
      this.axios
        .put("/api/admin/menus/hidden/" + menu.id, param)
        .then(({ data }) => {
          if (data.flag) {
            this.$notify.success({
              title: "成功",
              message: data.message,
            });
            this.listMenus();
          } else {
            this.$notify.error({
              title: "失败",
              message: data.message,
            });
          }
        });
    },
    listMenus() {
      this.axios
        .get("/api/admin/menus", {
          params: {
            keywords: this.keywords
          }
        })
        .then(({ data }) => {
          this.menuList = data.data;
          this.loading = false;
        });
    },
    openModel(menu) {
      var flag = true;

      if (menu != null) {
        if (menu > 0) {
          //若传入的是ID ，则表示新增子菜单
          this.type = false;
          this.menuForm.parentId = menu;
          this.isCatalog = false;
        } else {
          //若传入字符串数据，则表示修改菜单
          flag = false;
          this.menuForm = JSON.parse(JSON.stringify(menu));
          this.icon = this.menuForm.icon;
          if (this.menuForm.parentId == -1) {
            this.isCatalog = true;
          } else {
            this.isCatalog = false;
          }
          this.$refs.menuTitle.innerHTML = "修改菜单";
          this.updateMenu = false;
        }
      }
      if (flag) {
        //若传入为NULL ，则表示新增主菜单
        if (menu == null) {
          this.type = true;
          this.menuForm.parentId = null;
          this.isCatalog = true;
        }
        this.menuForm.id = null;
        this.menuForm.isHidden = null;
        this.menuForm.name = "";
        this.menuForm.icon = "";
        this.menuForm.component = "";
        this.menuForm.path = "";
        this.menuForm.orderNum = 1;
        this.menuForm.isDisable = null;
        this.menuForm.createTime = null;
        this.menuForm.updateTime = null;

        this.icon = "";
        this.updateMenu = true;
        this.$refs.menuTitle.innerHTML = "添加菜单";
      }

      this.showIcon = false;
      this.addMenu = true;
    },
    checkIcon(icon) {
      this.menuForm.icon = icon;
      this.showIcon = false;
    },
    saveOrUpdateMenu() {
      if (this.menuForm.name.trim() == "") {
        this.$message.error("菜单名不能为空");
        return false;
      }
      if (this.menuForm.icon.trim() == "") {
        this.$message.error("菜单icon不能为空");
        return false;
      }
      if (this.menuForm.component.trim() == "" && !this.isCatalog) {
        this.$message.error("菜单组件路径不能为空");
        return false;
      }
      if (this.menuForm.path.trim() == "") {
        this.$message.error("菜单访问路径不能为空");
        return false;
      }
      if (this.menuForm.isHidden == null) {
        this.$message.error("未勾选显示状态");
        return false;
      }
      this.menuForm.isCatalog = this.isCatalog;
      this.axios.post("/api/admin/menus", this.menuForm).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: "成功",
            message: data.message,
          });
          this.listMenus();
        } else {
          this.$notify.error({
            title: "失败",
            message: data.message,
          });
        }
        this.addMenu = false;
      });
    },
    deleteMenu(id) {
      this.axios.delete("/api/admin/menus/" + id).then(({ data }) => {
        if (data.flag) {
          this.$notify.success({
            title: "成功",
            message: "删除成功"
          });
          this.listMenus();
        } else {
          this.$notify.error({
            title: "失败",
            message: data.message
          });
        }
        this.deleteFlag = false;
      });
    }
  }
};
</script>

<style scoped>
.icon-item {
  cursor: pointer;
  padding: 0.5rem 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.menu-container {
  position: absolute;
  width: 90%;
  background: #fff;
  border-radius: 4px;
  border: 1px solid #e6ebf5;
  padding: 12px;
  z-index: 2000;
  color: #606266;
  text-align: justify;
  font-size: 14px;
  -webkit-box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  word-break: break-all;
  overflow-y: auto;
  height: 200px;
}
.menu-container div {
  cursor: pointer;
}
</style>
